package com.ywjh.inter.service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

import com.ywjh.inter.entity.*;
import com.ywjh.inter.utils.HdwDataCheckUtils;
import com.ywjh.inter.utils.StrUtil;

public class BioLandService {
	private static BioLandService instance;

	public static BioLandService getInstance() {
		if (instance == null) {
			instance = new BioLandService();
		}
		return instance;
	}

	public boolean checkReceivedData(String data) {

		if ((data != null) && ((data.length() == 85) || (data.length() == 87))) {

			if ((data.length() == 85) && (HdwDataCheckUtils.isSumCheckCode(data))) {
				return true;
			}

			if ((data.length() == 87) && (HdwDataCheckUtils.isSumCheckCode(data))
					&& (HdwDataCheckUtils.isCrc8Code(data))) {
				return true;
			}
		}
		return false;
	}

	public boolean checkReceivedData4G(String data) {
		System.out.println(data.length());
		if(data!=null && (data.toUpperCase().contains("GLUC") || data.toUpperCase().contains("UA") || data.toUpperCase().contains("CHOL"))){
			return true;
		}
		return false;
	}

	public boolean blglu(SrcDataValue srcDatavalue) {
		boolean b = false;
		List<EquBindEntity> list = EquBindService.getInstance().list(srcDatavalue.getSerialNumber(),
				EquBindService.TYPE_BGLU);
		if (!list.isEmpty()) {
			EquBindEntity equBindEntity = list.get(0);
			float glu = StrUtil.getFloat(srcDatavalue.getData3(), 0.0f);
			glu = glu / 18;
			BigDecimal decimal = new BigDecimal(glu);
			glu = decimal.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
			String memo = "Bioland_bpres";
			Timestamp time = StrUtil.getTimeStampYYMMDDHHMI(srcDatavalue.getYear() + srcDatavalue.getMonth()+srcDatavalue.getDay()
					+ srcDatavalue.getHour() + srcDatavalue.getMinute());
			int planId = UsersService.getInstance().getCurrentBgluPlanId(time, equBindEntity.getUser_id());
			BloodGlucoseEntity bg = new BloodGlucoseEntity();
			bg.setCheck_time(time);
			bg.setAdd_time(new Timestamp(System.currentTimeMillis()));
			bg.setData(glu);
			bg.setStatus(1);
			bg.setUser_id(equBindEntity.getUser_id());
			bg.setPlan_id(planId);
			bg.setMemo(memo);
			bg.setData_source("Bioland");
			System.out.println("Bioland bg="+bg.toString());
			if (PatientDataService.getInstance().addBloodGlucose(bg)) {
				b = true;
			} else {
				System.out.println("数据库故障");
			}
		} else {
			System.out.println("未找到Bioland血糖设备：" + srcDatavalue.getSerialNumber());
		}
		return b;
	}

	public boolean blglu4G(SrcDataValue4G dataValue4G) {

		boolean b = false;
		List<EquBindEntity> list = EquBindService.getInstance().list(dataValue4G.getSn(),
				EquBindService.TYPE_BGLU);
		if (!list.isEmpty()) {
			EquBindEntity equBindEntity = list.get(0);
			float glu = StrUtil.getFloat(dataValue4G.getData(), 0.0f);
			//glu = glu / 18;
			BigDecimal decimal = new BigDecimal(glu);
			glu = decimal.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
			String memo = "设备通过4G经socket通信上传数据";

			Timestamp time = StrUtil.getTimeStampYYMMDDHHMI(dataValue4G.getYear() + dataValue4G.getMonth()+dataValue4G.getDay()
					+ dataValue4G.getHour() + dataValue4G.getMinute());
			int planId = UsersService.getInstance().getCurrentBgluPlanId(time, equBindEntity.getUser_id());
			BloodGlucoseEntity bg = new BloodGlucoseEntity();
			bg.setCheck_time(time);
			bg.setAdd_time(new Timestamp(System.currentTimeMillis()));
			bg.setData(glu);
			bg.setStatus(1);
			bg.setUser_id(equBindEntity.getUser_id());
			bg.setPlan_id(planId);
			bg.setMemo(memo);
			bg.setData_source("SOCKET_4G");
			System.out.println("4G bg="+bg.toString());
			if (PatientDataService.getInstance().addBloodGlucose(bg)) {
				b = true;
			} else {
				System.out.println("数据库故障");
			}
		} else {
			System.out.println("未找到血糖设备：" + dataValue4G.getSn());
		}
		return b;
	}

	public boolean bpress(SrcDataValue srcDatavalue) {
		boolean b = false;
		List<EquBindEntity> list = EquBindService.getInstance().list(srcDatavalue.getSerialNumber(),
				EquBindService.TYPE_BPRE);
		if (!list.isEmpty()) {
			EquBindEntity equBindEntity = list.get(0);
			float hi = StrUtil.getInt(srcDatavalue.getData1(), 0);
			float low = StrUtil.getInt(srcDatavalue.getData2(), 0);
			String memo = "Bioland_bglu";
			int pulse = StrUtil.getInt(srcDatavalue.getData3(), 0);
			Timestamp time = StrUtil.getTimeStampYYMMDDHHMI(srcDatavalue.getYear() + srcDatavalue.getMonth()+srcDatavalue.getDay()
			+ srcDatavalue.getHour() + srcDatavalue.getMinute());
			BloodPressureEntity bloodPressureEntity = new BloodPressureEntity();
			bloodPressureEntity.setCheck_time(time);
			bloodPressureEntity.setAdd_time(new Timestamp(System.currentTimeMillis()));
			bloodPressureEntity.setHi(hi);
			bloodPressureEntity.setLow(low);
			bloodPressureEntity.setPulse(pulse);
			bloodPressureEntity.setStatus(1);
			bloodPressureEntity.setUser_id(equBindEntity.getUser_id());
			bloodPressureEntity.setMemo(memo);
			bloodPressureEntity.setData_source("Bioland");
			if (PatientDataService.getInstance().addBloodPressure(bloodPressureEntity)) {
				b = true;
			} else {
				System.out.println("数据库故障");
			}
		} else {
			System.out.println("未找到Bioland血压设备：" + srcDatavalue.getSerialNumber());
		}
		return b;
	}
}
